<?php
/**
 * Created by PhpStorm.
 * @author 清凉Tea <973110519@qq.com>
 * Date: 2018/1/20
 * Time: 16:42
 */

namespace app\api\validate;
use think\Request;
use think\Validate;

/**
 * 验证器 统一验证方式 goCheck
 */
class BaseValidate extends Validate
{

    public function goCheck(){
        $params = Request::instance()->param();
        $result = $this->batch()->check($params);
        if(!$result){
            $e = new \Exception([
                'msg' => $this->error,
            ]);
            throw $e;
        }else{
            return true;
        }
    }

    /**
     * 判断是否为正整数
     * @return bool
     * @author 清凉Tea <973110519@qq.com>
     */
    protected function isPositiveInteger($value, $rule = '', $data = '', $field = '')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0){
            return true;
        }else{
            return false;
        }
    }

    public function isNotEmpty($value)
    {
        if(empty($value))
            return false;
        else
            return true;
    }

    //没有使用TP的正则验证，集中在一处方便以后修改
    //不推荐使用正则，因为复用性太差
    //手机号的验证规则
    protected function isMobile($value)
    {
        $rule = '^1(3|4|5|7|8)[0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}